package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

 public R<Employee> login(HttpServletRequest request, Employee employee) {
  String password;
  password = employee.getPassword();
  password = DigestUtils.md5DigestAsHex(password.getBytes());
  LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
  wrapper.eq(Employee::getUsername, employee.getUsername());
  Employee one = this.getOne(wrapper);
  if (one == null) {
   return R.error("登陆失败");
  }
  if (!one.getPassword().equals(password)) {
   return R.error("登陆失败");
  }
  if (one.getStatus() != 1) {
   return R.error("账号已禁用");
  }
  request.getSession().setAttribute("employee", one.getId());
  return R.success(one);
 }


 public R<String> logout(HttpServletRequest request) {
  request.getSession().removeAttribute("employee");
  return R.success("退出成功");
 }

 public R<Page> page(int page, int pageSize, String name) {
  log.info("page = {},pageSize = {},name = {}", page, pageSize, name);
  //构造分页构造器
  Page pageInfo = new Page(page, pageSize);
  //构造条件构造器
  LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
  //添加过滤条件
  queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
  //添加排序条件
  queryWrapper.orderByDesc(Employee::getUpdateTime);
  //执行查询
  this.page(pageInfo, queryWrapper);

  return R.success(pageInfo);
 }

 public R<String> insert(HttpServletRequest request, @RequestBody Employee employee) {
  employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
 /* employee.setCreateTime(LocalDateTime.now());
  employee.setUpdateTime(LocalDateTime.now());
  Long empID = (Long) request.getSession().getAttribute("employee");
  employee.setUpdateUser(empID);
  employee.setCreateUser(empID);*/
  this.save(employee);
  return R.success("新增成员成功");
 }

 public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
  log.info(employee.toString());
 /* Long empId = (Long)request.getSession().getAttribute("employee");
  employee.setUpdateTime(LocalDateTime.now());
  employee.setUpdateUser(empId);*/
  this.updateById(employee);
  return R.success("员工信息修改成功");
 }

 public R<Employee> get(@PathVariable Long id) {
  Employee employeeR = this.getById(id);
  if (employeeR != null) {
   return R.success(employeeR);
  }
  return R.error("没有查询到对应员工信息");
 }
}
